﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CryptoChat.Models;

namespace CryptoChat.Controllers
{
	[HandleError]
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			//	1st visit
			if (!CryptoChatRepository.UserExists(User.Identity.Name))
			{
				// If this user doesn't exist, create them!
				User freshUser = new User()
				{
					UserName = User.Identity.Name,
					ScreenName = User.Identity.Name,
					Avatar = System.IO.File.ReadAllBytes(Server.MapPath("/Content/avatar.png")),
					 LastCheckedIn = DateTime.Now,
				};

				CryptoChatRepository.AddUser(freshUser);

				//// TEST CODE
				//User template = CryptoChatRepository.GetUser(1);

				//User user = new User()
				//{
				//    UserName = "Alpha",
				//    ScreenName = "AlphaMale",
				//    Avatar = template.Avatar,
				//    LastCheckedIn = DateTime.Now.Subtract(new TimeSpan(0, 10, 0)),
				//};

				//CryptoChatRepository.AddUser(user);

				//user = new User()
				//{
				//    UserName = "Beta",
				//    ScreenName = "BetaTester",
				//    Avatar = template.Avatar,
				//    LastCheckedIn = DateTime.Now,
				//};

				//CryptoChatRepository.AddUser(user);
				//// TEST CODE

				ViewData["Message"] = "Welcome, this is your 1st visit to CryptoChat! " + freshUser.ScreenName;
			}
			//	Return visit
			else
			{
				User user = CryptoChatRepository.GetUser(User.Identity.Name);

				ViewData["Message"] = "Welcome back to CryptoChat! " + user.ScreenName;
			}

			ChatViewModel model = new ChatViewModel();

			model.OtherUsers = CryptoChatRepository.GetOtherUsers(User.Identity.Name);

			return View("Chat", model);
		}

		public ActionResult About()
		{
			return View();
		}
	}
}
